package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取会员班级（老师班级）信息
func GetUserClassInfo(p map[string]interface{}) *models.UserClass {
	o := orm.NewOrm()
	userClass := &models.UserClass{}
	qs := o.QueryTable("UserClass")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["classId"]; ok {
		qs = qs.Filter("class_id", p["classId"])
	}
	qs.One(userClass)
	return userClass
}

// 添加会员班级（老师班级）信息
func AddUserClass(input url.Values) (int64, error) {
	o := orm.NewOrm()
	userClass := &models.UserClass{}

	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	userClass.User = user

	class := &models.Class{}
	if _, ok := input["class_id"]; ok {
		class.Id = utils.Atoi64(input["class_id"][0])
		o.Read(class)
	}
	userClass.Class = class

	course := &models.Course{}
	if _, ok := input["course_id"]; ok {
		course.Id = utils.Atoi64(input["course_id"][0])
		o.Read(course)
	}
	userClass.Course = course

	return o.Insert(userClass)
}